<?php

namespace App\Http\Middleware;

use Closure;
use Validator;
use Illuminate\Support\Facades\DB;

class checkLoginMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     *
     *
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        $where['admin_token'] =  $request->input('admin_token');
        $where['admin_name'] =  $request->input('admin_name');

        $validator = Validator::make($where, [
            'admin_name' => 'required',
            'admin_token' => 'required',
        ]);
        if ($validator->fails()) {
            return 0 ;
        }

        $result = DB::table('admin')->where($where)->first();

        if($result){
            return $next($request);
        }else{
            return 0;
        }

    }
}
